package com.itic.web.mem.member.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class FormDataUtil {
	
	//传图片
	public static String inputStreamUpload(String url,InputStream inputStream,String userId,String ind,String fileType,String tableName) {
		
		String result = null;
	    //创建HttpClient对象  
	    CloseableHttpClient client = HttpClients.createDefault();  
	    //构建POST请求   请求地址请更换为自己的。  
	    //1)  
	    HttpPost post = new HttpPost(url);  
	    try {  
	        MultipartEntityBuilder builder = MultipartEntityBuilder.create();  
	        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);  
	        //第一个参数为 相当于 Form表单提交的file框的name值 第二个参数就是我们要发送的InputStream对象了  
	        //第三个参数是文件名  
	        //3)  
	        builder.addBinaryBody("uploadFile", inputStream, ContentType.create("multipart/form-data"), "22.png");  
	        //4)构建请求参数 普通表单项  
	        //StringBody stringBody = new StringBody("12",ContentType.MULTIPART_FORM_DATA);  
	        //builder.addPart("id",stringBody);  
	        builder.addPart("userId",new StringBody(userId,ContentType.MULTIPART_FORM_DATA));  
	        builder.addPart("ind",new StringBody(ind,ContentType.MULTIPART_FORM_DATA));  
	        builder.addPart("fileType",new StringBody(fileType,ContentType.MULTIPART_FORM_DATA));  
	        builder.addPart("tableName",new StringBody(tableName,ContentType.MULTIPART_FORM_DATA));  
	        HttpEntity entity = builder.build();  
	        post.setEntity(entity);  
	        //发送请求  
	        HttpResponse response = client.execute(post);  
	        entity = response.getEntity();  
	        if (entity != null) {  
	            inputStream = entity.getContent();  
	            //转换为字节输入流  
	            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, Consts.UTF_8));  
	            String body = null;  
	            while ((body = br.readLine()) != null) {  
	                System.out.println(body);
	                result = body;
	            }  
	        }  
	    } catch (FileNotFoundException e) {  
	        e.printStackTrace();  
	    } catch (ClientProtocolException e) {  
	        e.printStackTrace();  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    }finally {  
	        if(inputStream != null){  
	            try {  
	                inputStream.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	    }
	    
	    
	    FileResult ro=new Main<FileResult>().jsonConverObject(result, FileResult.class);// 流方式  
        System.out.println("转对象里有集合类 ro :" + ro.getMessage());
        FileData data = ro.getDatas();
        System.out.println("返回的图片地址是:"+ data.getUrl());

	    return data.getUrl();
	}
	
	//传文件
	 public  static void fileUpload() {  
	     //构建HttpClient对象  
	     CloseableHttpClient client = HttpClients.createDefault();  
	     //构建POST请求  
	     HttpPost httpPost = new HttpPost("http://localhost:8003/uploadAndDownload/uploadFileAction");  
	     InputStream inputStream = null;  
	     try {  
	         //要上传的文件  
	         File file = null; file = new File("G:\\qqq.txt");  
	         //构建文件体  
	         FileBody fileBody = new FileBody(file,ContentType.MULTIPART_FORM_DATA,"qqq.txt");  
	         StringBody stringBody = new StringBody("12",ContentType.MULTIPART_FORM_DATA);  
	         HttpEntity httpEntity = MultipartEntityBuilder  
	                 .create()  
	                 .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)  
	                 .addPart("uploadFile", fileBody)  
	                 .addPart("id",stringBody).build();  
	         httpPost.setEntity(httpEntity);  
	         //发送请求  
	         HttpResponse response = client.execute(httpPost);  
	         httpEntity = response.getEntity();  
	         if(httpEntity != null){  
	             inputStream = httpEntity.getContent();  
	             //转换为字节输入流  
	             BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, Consts.UTF_8));  
	             String body = null;  
	             while ((body = br.readLine()) != null) {  
	                 System.out.println(body);  
	             }  
	         }  
	     } catch (IOException e) {  
	         e.printStackTrace();  
	     } finally {  
	         if(inputStream != null){  
	             try {  
	                 inputStream.close();  
	             } catch (IOException e) {  
	                 e.printStackTrace();  
	             }  
	         }  
	     }  
	 } 
	


}
